package com.example.minaim;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import android.R.bool;
import android.R.integer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.example.minaim.ClientHandler.Callback;
import com.example.minaim.Define.ClientStatus;
import com.example.minaim.MainActivity.MyReceiver;

public class battleActivity extends Activity {


	private Define define;
	
	private MyReceiver receiver;
	
	private User userInfo;
	//private List<BaseSkill> baseskills;
	private HashMap<String, BaseSkill> baseskills;
	private List<BaseSkill> baseSkillsList;
	private List<Skill> allSkillsList;
	
	public ClientStatus clientStatus = ClientStatus.idle;

	public TextView myHpBar;
	public TextView myMpBar;
	public TextView myStBar;
	public TextView myDeBar;
	public TextView enemyHpBar;
	public TextView enemyMpBar;
	public TextView enemyStBar;
	public TextView enemyDeBar;
	public TextView txtBattleInformation;
	
	private battleInfo mybattleInfo;
	private Boolean mybattleInfo_flag;	
	
	private battleInfo enemybattleInfo;
	private Boolean enemybattleInfo_flag;
	
	private battleInfo mybattleInfo_temp;
	private battleInfo enemybattleInfo_temp;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//鍘绘帀鏍囬鏍�
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				  WindowManager.LayoutParams.FLAG_FULLSCREEN);//璁剧疆鍏ㄥ睆
		super.onCreate(savedInstanceState);
		setContentView(R.layout.battle);
		//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		mybattleInfo = new battleInfo();
		enemybattleInfo = new battleInfo();
		
		mybattleInfo_temp = new battleInfo();
		enemybattleInfo_temp = new battleInfo();
		mybattleInfo_flag = false;
		enemybattleInfo_flag = false;
		
		
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();

		baseSkillsList = (List<BaseSkill>) bundle.getSerializable("baseSkillsList");
		userInfo = (User) bundle.getSerializable("userInfo");
		allSkillsList = (List<Skill>) bundle.getSerializable("allSkillsList");
		
		receiver = new MyReceiver();                
		IntentFilter filter = new IntentFilter();		
		filter.addAction("com.minam.test");		
		battleActivity.this.registerReceiver(receiver, filter);	
		
		myHpBar = (TextView) findViewById(R.id.progress_hp);
		myMpBar = (TextView) findViewById(R.id.progress_mp);

		enemyHpBar = (TextView) findViewById(R.id.progress_enemyhp);

		enemyStBar = (TextView) findViewById(R.id.progress_enemyst);

		
		txtBattleInformation = (TextView) findViewById(R.id.textBattleInformation);
		
		
		//BATTLEJOIN
        String BATTLEJOINCmd="BATTLEJOIN";	
		Intent intentBATTLEJOIN = new Intent();	
		intentBATTLEJOIN.putExtra("cmd","send");
		intentBATTLEJOIN.putExtra("body",BATTLEJOINCmd);
		intentBATTLEJOIN.setAction("com.minam.client");// action涓庢帴鏀跺櫒鐩稿悓		
		sendBroadcast(intentBATTLEJOIN);
		
		clientStatus = ClientStatus.battlejoin;
	}
	
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检测屏幕的方向：纵向或横向
        if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            //当前为横屏， 在此处添加额外的处理代码
        }
        else if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏， 在此处添加额外的处理代码
        }
        //检测实体键盘的状态：推出或者合上
        if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_NO){
            //实体键盘处于推出状态，在此处添加额外的处理代码
        }
        else if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_YES){
            //实体键盘处于合上状态，在此处添加额外的处理代码
        }
    }
	
	public class MyReceiver extends BroadcastReceiver
    {
        
        // 鑷畾涔変竴涓箍鎾帴鏀跺櫒
        
        @Override
        public void onReceive(Context context, Intent intent)
        {
            
            // TODO Auto-generated method stub
            
            System.out.println("tianfuActivity OnReceiver");
            
            Bundle bundle = intent.getExtras();
            
            String cmd = bundle.getString("cmd");
            
            if (cmd.equals("receive")) {
            	String body = bundle.getString("body");
            	Cmd2vector cmd2vector = new Cmd2vector();
            	cmd2vector.cmd2vector(body);
            	
            	//“c08&f=0”
            	if (cmd2vector.cmd.equals("c08")) {
					String result = cmd2vector.paras.get("f");
            		//c05&total=2&number=1&name=濮濓箒鈧懎銇夌挧?desc=閹碘偓閺堝鏁鹃崙缁樺Η閼宠姤鏁鹃崙璇插婢х偛濮�%
            		if (result.equals("0")){
            			clientStatus = ClientStatus.battlewait;
            			Toast.makeText(battleActivity.this, "等待其他人进入游戏", 2000).show();
					}
            		else {
            			Toast.makeText(battleActivity.this, "服务器已经满了，请等待", 2000).show();
					}
				}
            	
            	//“c0e&f=0”
            	if (cmd2vector.cmd.equals("c0e")) {
					String result = cmd2vector.paras.get("f");
            		//c05&total=2&number=1&name=濮濓箒鈧懎銇夌挧?desc=閹碘偓閺堝鏁鹃崙缁樺Η閼宠姤鏁鹃崙璇插婢х偛濮�%
            		if (result.equals("0")){
            			clientStatus = ClientStatus.battleing;
            			//Toast.makeText(battleActivity.this, "等待其他人进入游戏", 2000).show();
					}
				}
            	
            	//“c0b&id=1&n=姓名&male=男&png=1.png&hp=20&mp=5&at=5&de=5&shengyupoint=10”
            	if (cmd2vector.cmd.equals("c0b")) {
            		if (mybattleInfo_flag == false) {
    					mybattleInfo.set_hp(Integer.parseInt(cmd2vector.paras.get("Hp")));    					
    					mybattleInfo.set_mp(Integer.parseInt(cmd2vector.paras.get("Mp")));
    					mybattleInfo.set_st(Integer.parseInt(cmd2vector.paras.get("St")));
    					mybattleInfo.set_de(Integer.parseInt(cmd2vector.paras.get("De")));
    					
    					mybattleInfo.set_baseskill(cmd2vector.paras.get("Baseskill"));
    					mybattleInfo.set_skills(cmd2vector.paras.get("Skills"));
    					
    					mybattleInfo.set_nickname(cmd2vector.paras.get("Nickname"));
    					mybattleInfo.set_sex(cmd2vector.paras.get("Sex"));
    					
    					mybattleInfo.set_totalfailed(Integer.parseInt(cmd2vector.paras.get("Totalfailed")));
    					mybattleInfo.set_totalvictory(Integer.parseInt(cmd2vector.paras.get("Totalvictory")));
    					mybattleInfo_flag = true;
    					
    					//myHpBar.setMax(mybattleInfo.get_hp());
    					
    					
					}
            		else {
    					mybattleInfo_temp.set_hp(Integer.parseInt(cmd2vector.paras.get("Hp")));
    					mybattleInfo_temp.set_mp(Integer.parseInt(cmd2vector.paras.get("Mp")));
    					mybattleInfo_temp.set_st(Integer.parseInt(cmd2vector.paras.get("St")));
    					mybattleInfo_temp.set_de(Integer.parseInt(cmd2vector.paras.get("De")));
    					
    					mybattleInfo_temp.set_baseskill(cmd2vector.paras.get("Baseskill"));
    					mybattleInfo_temp.set_skills(cmd2vector.paras.get("Skills"));
    					
    					mybattleInfo_temp.set_nickname(cmd2vector.paras.get("Nickname"));
    					mybattleInfo_temp.set_sex(cmd2vector.paras.get("Sex"));
    					
    					mybattleInfo_temp.set_totalfailed(Integer.parseInt(cmd2vector.paras.get("Totalfailed")));
    					mybattleInfo_temp.set_totalvictory(Integer.parseInt(cmd2vector.paras.get("Totalvictory")));
						
    					//myHpBar.setSecondaryProgress(myHpBar.getProgress());
    					//myHpBar.setProgress(mybattleInfo_temp.get_hp());
					}
						
				}
            	
            	// cmd = "c0c&f="+myBattleInfo;
            	if (cmd2vector.cmd.equals("c0c")) {
            		txtBattleInformation.setText(txtBattleInformation.getText() +  cmd2vector.paras.get("f"));
				}
            	
            	
            	//“c0a&id=1&n=姓名&male=男&png=1.png&hp=20&mp=5&at=5&de=5&shengyupoint=10”
            	if (cmd2vector.cmd.equals("c0a")) {
            		
            		if (enemybattleInfo_flag == false) {
                		enemybattleInfo.set_hp(Integer.parseInt(cmd2vector.paras.get("Hp")));
                		enemybattleInfo.set_mp(Integer.parseInt(cmd2vector.paras.get("Mp")));
                		enemybattleInfo.set_st(Integer.parseInt(cmd2vector.paras.get("St")));
                		enemybattleInfo.set_de(Integer.parseInt(cmd2vector.paras.get("De")));
    					
                		enemybattleInfo.set_baseskill(cmd2vector.paras.get("Baseskill"));
                		enemybattleInfo.set_skills(cmd2vector.paras.get("Skills"));
    					
                		enemybattleInfo.set_nickname(cmd2vector.paras.get("Nickname"));
                		enemybattleInfo.set_sex(cmd2vector.paras.get("Sex"));
    					
                		enemybattleInfo.set_totalfailed(Integer.parseInt(cmd2vector.paras.get("Totalfailed")));
                		enemybattleInfo.set_totalvictory(Integer.parseInt(cmd2vector.paras.get("Totalvictory")));
    						
            			enemybattleInfo_flag = true;
            			
            			//enemyHpBar.setMax(enemybattleInfo.get_hp());
					} else {
	            		enemybattleInfo_temp.set_hp(Integer.parseInt(cmd2vector.paras.get("Hp")));
	            		enemybattleInfo_temp.set_mp(Integer.parseInt(cmd2vector.paras.get("Mp")));
	            		enemybattleInfo_temp.set_st(Integer.parseInt(cmd2vector.paras.get("St")));
	            		enemybattleInfo_temp.set_de(Integer.parseInt(cmd2vector.paras.get("De")));
						
	            		enemybattleInfo_temp.set_baseskill(cmd2vector.paras.get("Baseskill"));
	            		enemybattleInfo_temp.set_skills(cmd2vector.paras.get("Skills"));
						
	            		enemybattleInfo_temp.set_nickname(cmd2vector.paras.get("Nickname"));
	            		enemybattleInfo_temp.set_sex(cmd2vector.paras.get("Sex"));
						
	            		enemybattleInfo_temp.set_totalfailed(Integer.parseInt(cmd2vector.paras.get("Totalfailed")));
	            		enemybattleInfo_temp.set_totalvictory(Integer.parseInt(cmd2vector.paras.get("Totalvictory")));
	            		//enemyHpBar.setSecondaryProgress(enemyHpBar.getProgress());
	            		//enemyHpBar.setProgress(enemybattleInfo_temp.get_hp());		
					}

				}
            }

        }
        
        public MyReceiver()
        {
            
            System.out.println("MyReceiver");
            
            // 鏋勯�鍑芥暟锛屽仛涓�簺鍒濆鍖栧伐浣滐紝鏈緥涓棤浠讳綍浣滅敤
            
        }
        
    }
	
}
